package com.daydayup.learn.zikao;

import cn.hutool.core.util.StrUtil;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.stream.Stream;

public class MyDirFile {

    private Frame frame = new Frame();
    private Button button = new Button("转到");
    private TextField textField = new TextField(50);
    private TextArea textArea = new TextArea(20,50);
    private List<String> rs = new ArrayList<>();

    public MyDirFile(){
        init();
    }

    private void init(){
        frame.setBounds(100,100,500,500);
        frame.setLayout(new FlowLayout());
        frame.add(button);
        frame.add(textField);
        textArea.setForeground(Color.BLACK);
        textArea.setFont(new Font("微软雅黑",10,15));
        frame.add(textArea);
        myEvent();
        frame.setVisible(true);
    }

    private void myEvent() {
        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("窗口打开了");
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("窗口关闭中......");
                System.exit(1);
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 实现逻辑,去展示目录及其子目录
                shouDir();
            }
        });
        textField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println("keyEvent = "+e.getKeyLocation());
            }

            @Override
            public void keyPressed(KeyEvent e) {
              // System.out.println("keyPressed = "+e.getKeyCode());
                if (e.getKeyCode() == 10){
                    System.out.println("=========XXXXXXXXXXX");
                    shouDir();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("key 释放......");
            }
        });
    }

    private void shouDir() {
        String dir = textField.getText();
        rs.clear();
        try {
            //printFile(dir);
            //walkFileTree(dir);
            //walk(dir);
            queue(dir);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        textArea.setText(StrUtil.join("\n",rs));
    }

    /**
     * 通过递归的方式去获取目录及其子孙目录的文件并写入到list中去
     * @param path
     */
    private void printFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                rs.add("目录:"+path);
                for (File f : file.listFiles()) {
                    if (f.isDirectory()) {
                        printFile(f.getAbsolutePath());
                    } else {
                        rs.add("文件:"+f.getAbsolutePath());
                    }
                }
            } else {
                rs.add("文件:"+path);
            }
        }
    }

    private void walkFileTree(String path) throws IOException {
        Path start = Paths.get(path);
        Files.walkFileTree(start,new SimpleFileVisitor() {
            @Override
            public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
                //rs.add("pre 目录:"+dir.toString());
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
                rs.add("文件:"+file.toString());
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException {
                //rs.add("");
                System.out.println("访问失败:"+exc.getMessage());
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException {
                rs.add("目录:"+dir.toString());
                return FileVisitResult.CONTINUE;
            }
        });
    }

    private void walk(String path) throws IOException {
        Path start = Paths.get(path);
        try(Stream<Path> stream = Files.walk(start)) {
            stream.forEach((s) -> {
                rs.add(s.toAbsolutePath().toString());
            });
        }
    }

    private void queue(String path) {
        File file = new File(path);
        Queue<File> queue = new LinkedList<>();
        queue.offer(file);
        while (!queue.isEmpty()) {
            File dir = queue.poll();
            for (File f : dir.listFiles()) {
                if (f.isDirectory()) {
                    rs.add("目录:"+f.getAbsolutePath());
                    queue.offer(f);
                } else {
                    rs.add("文件:"+f.getAbsolutePath());
                }
            }
        }
    }

}
